<template>
    <el-button title="暂时保存添加的数据，下次添加时回显上次暂不提交的数据！" type="warning" :plain="plain" @click="save">{{ name }}</el-button>
</template>
<script>
import draftBox from '@/api/draftBox.js';
export default {
    components: {},
    props: {
        type: {
            type: String,
            default: 'warning'
        },
        plain: {
            type: Boolean,
            default: false
        },
        name: {
            type: String,
            default: '暂不提交'
        },
        formData: {
            type: Object,
            default: () => {
                return {}
            }
        }
    },
    data() {
        return {}
    },
    mounted() {
        this.queryByKey();
    },
    methods: {
        delByKey(){
            let key = this.$route.path;
            draftBox.delByKey(key)
        },
        save() {
            if (Object.keys(this.formData).length == 0) {
                this.$message.warning('提交保存数据不能为空')
                return
            }
            let key = this.$route.path;
            let content = JSON.stringify(this.formData);
            draftBox.add({ key, content }).then(res => {
                if (res.success) {
                    this.$message.success('提交成功')
                } else {
                    this.$message.warning(res.message || '提交失败')
                }
            }).catch(err => {
                console.log(err)
            })
        },
        queryByKey() {
            let key = this.$route.path;
            draftBox.queryByKey(key).then(res => {
                if (res.success && res.data) {
                    this.$emit('pushData', JSON.parse(res.data))
                }else{
                    console.log(res)
                }
            }).catch(err => {
                console.log(err)
            })
        },
    },


};
</script>